<template>
  <div :class="userMiss?'maxSize_miss':'maxSize'" >
    <el-form>
      <el-form-item label="题目内容" >
        <el-text>{{props.question.content}}</el-text>
      </el-form-item>
      <el-form-item label="题目类型" prop="type">
        <el-text>{{questionTypes[props.question.type]}}题</el-text>
      </el-form-item>

      <el-form-item label="题目答案" prop="answer">
        <ChoiceAnswerShow :user-answer="{list:userAnswerList,miss:userMiss}" :question="props.question"></ChoiceAnswerShow>
      </el-form-item>

      <el-form-item label="题目标签">
        <el-tag v-for="item in allTag">{{item.name}}</el-tag>
      </el-form-item>

      <el-form-item label="正确答案">
        <div v-for="item in questionAnswerList">
          <el-text type="primary"  v-if="item.isRight">{{item.answer}}</el-text>
        </div>
      </el-form-item>

      <el-form-item label="你的答案">
        <el-text  v-if="userAnswerList.length<=0">该题未做</el-text>
        <div v-else>
          <el-text v-for="item in userAnswerList" v-if="!userMiss" type="success">{{item.answer}}</el-text>
          <el-text v-for="item in userAnswerList" v-else type="danger">{{item.answer}}</el-text>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import {onMounted, reactive, ref} from "vue";
import {Exam, Question, QuestionAnswer, questionTypes, Tag, User, UserAnswer} from "../../types/mytypes";
import {FormInstance, FormRules} from "element-plus";
import ChoiceAnswer from "./ChoiceAnswer.vue";
import {QuestionRequest, TagRequest} from "../../request/MyRequset";
import {UserStore} from "../../store/store";
import ChoiceAnswerShow from "./ChoiceAnswerShow.vue";
const props = defineProps<{
  question:Question,
  user:User,
  exam:Exam
}>()
const formRef = ref<FormInstance>()
const emit = defineEmits(["error", "success"]);
let userAnswerList = ref<UserAnswer[]>([])
let questionAnswerList = ref<QuestionAnswer[]>([])
let userMiss = ref(false)

//全部标签
let allTag = ref<Tag[]>([])

//====================FN====================

onMounted(async () => {
  allTag.value = (await TagRequest.getByQuestion(props.question.id as number)).data
  questionAnswerList.value = (await QuestionRequest.getAllAnswer(props.question.id as number)).data
  userAnswerList.value = (await QuestionRequest.getUserQuestionAnswer(props.user.id,props.exam.id,props.question.id as number)).data
  let rightRes = await QuestionRequest.userIsRight(props.user.id,props.exam.id,props.question.id as number)
  userMiss.value = !rightRes.data
  //console.log(userMiss.value)
  //构建显示在页面上的答案
  let uaList = userAnswerList.value
  let qaList = questionAnswerList.value
  //console.log(uaList)
})

const formRules = reactive<FormRules>({
  content: [
    {type: "string", required: true, message: "不能为空"}
  ],
  type: [
    {type: "number", required: true, message: "不能为空"}
  ],
})

const error = (error: string) => {
  emit("error", error);
}
const success = (msg?: string) => {
  emit("success", msg);
}
</script>

<style scoped lang="scss">
.maxSize {
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
  padding: 10px;
  width: 40em;
  margin-top: 10px;
}
.maxSize_miss{
  border: 2px solid red;
  border-radius: 4px;
  box-sizing: border-box;
  padding: 10px;
  width: 40em;
  margin-top: 10px;
}
</style>